﻿using System;
using System.Collections.Generic;

using UnityEngine;


public class KejianCfgData : ImplData
{

    public class KejianCfg
    {
        public List<string> kejian = new List<string>();
        public List<string> audio = new List<string>();
        public List<string> video = new List<string>();
        public List<string> drum = new List<string>();
        public List<string> dancing = new List<string>();
    }

    public Dictionary<string, KejianCfg> d = new Dictionary<string, KejianCfg>();

    protected override int Deserialize()
    {
        d.Clear();

        if (string.IsNullOrEmpty(jsonText))
            return -1;

        data = MiniJSON.Json.Deserialize(jsonText) as Dictionary<string, object>;

        foreach (var item in data)
        {
            KejianCfg cfg = new KejianCfg();

            Dictionary<string, object> dict = item.Value as Dictionary<string, object>;
            foreach( var i in dict)
            {
                List<object> tmp = i.Value as List<object>;
                foreach( var ii in tmp )
                {
                    if ( i.Key == "kejian" )
                    {
                        cfg.kejian.Add(ii.ToString());
                    }
                    else if ( i.Key == "audio")
                    {
                        cfg.audio.Add(ii.ToString());
                    }
                    else if ( i.Key == "video")
                    {
                        cfg.video.Add(ii.ToString());
                    }
                    else if ( i.Key == "drum")
                    {
                        cfg.drum.Add(ii.ToString());
                    }
                    else if ( i.Key == "dancing" )
                    {
                        cfg.dancing.Add(ii.ToString());
                    }
                }
            }

            d.Add(item.Key, cfg);

        }

        return 0;
    }

    public KejianCfg GetCfg(string id)
    {
        if (d.ContainsKey(id))
            return d[id];
        else
            return null;
    }

    public bool GetVideoUrl(string id, out string v, out string a)
    {
        v = null;
        a = null;
        KejianCfg k = GetCfg(id);

        foreach (var item in k.video)
        {
            if (item.EndsWith(".mp4") || item.EndsWith(".MP4") || item.EndsWith(".Mp4") )
                v = item;
            else
                a = item;
        }

        if (string.IsNullOrEmpty(v))
            return false;
        else
            return true;

    }

    public List<string> GetPicUrl(string id)
    {
        KejianCfg k = GetCfg(id);

        return k.kejian;

    }

    public List<string> GetAudioUrl(string id)
    {
        KejianCfg k = GetCfg(id);

        return k.audio;
    }


    public bool GetDrumUrl(string id, out string v, out string a)
    {
        v = null;
        a = null;
        KejianCfg k = GetCfg(id);

        foreach (var item in k.drum)
        {
            if (item.EndsWith(".mp4") || item.EndsWith(".MP4") || item.EndsWith(".Mp4"))
                v = item;
            else
                a = item;
        }

        if (string.IsNullOrEmpty(v))
            return false;
        else
            return true;

    }


    public bool GetDancingUrl(string id, out string v, out string a)
    {
        v = null;
        a = null;
        KejianCfg k = GetCfg(id);

        foreach (var item in k.dancing)
        {
            if (item.EndsWith(".mp4") || item.EndsWith(".MP4") || item.EndsWith(".Mp4"))
                v = item;
            else
                a = item;
        }

        if (string.IsNullOrEmpty(v))
            return false;
        else
            return true;

    }


}


